//
//  SYStatusTableViewCell.swift
//  YANAWeibo
//
//  Created by mac on 16/5/15.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

class SYStatusTableViewCell: UITableViewCell {
    /// 记录约束
    var toolBarTop: Constraint?
    
    //设置数据
    var statusViewModel: SYStatusViewModel? {
        didSet {
            originalView.statusViewModel = statusViewModel
            // 卸载约束
            toolBarTop?.uninstall()
            //判断是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil {
                //有转发微博
                retweetView.statusViewModel = statusViewModel
                retweetView.hidden = false
                // 更新约束
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else{
                // 无转发微博
                retweetView.hidden = true
                toolBar.snp_makeConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(originalView.snp_bottom).constraint
                })
                
            }
            
            // 绑定toolbar数据
            toolBar.statusViewModel = statusViewModel
         
        }
    }
    
    /// 原创微博视图
    private lazy var originalView: SYStatusOriginalView = SYStatusOriginalView()
    /// 设置toolbar
    private lazy var toolBar: SYStatusToolBar = SYStatusToolBar()
    /// 转发微博视图
    private lazy var retweetView: SYStatusRetweetView = SYStatusRetweetView()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        //设置约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        toolBar.snp_makeConstraints { (make) -> Void in
            // 记录约束
            self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
            make.trailing.equalTo(contentView)
            make.leading.equalTo(contentView)
            make.height.equalTo(40)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        //关键的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }

}
